package pap.bodies;

import java.util.List;

import pap.initialization.FileBodyInitializer;
import pap.initialization.IBodyInitializer;
import pap.initialization.RandomBodyInitializer;
import pap.initialization.TestBodyInitializer;
import pap.utils.UncompatibleDimensionsException;

public class BodiesManager {

	private static BodiesManager instance = null;

	List<Body> bodies;

	private BodiesManager(IBodyInitializer initializer) {

		bodies = initializer.initAndGetBodies();
	}

	/**
	 * Random initialization
	 */
	public static void initRandom() {

		instance = new BodiesManager(new RandomBodyInitializer());
	}

	/**
	 * Initialization from file
	 * 
	 * @param fileName
	 */
	public static void initFromFile(String fileName) {

		instance = new BodiesManager(new FileBodyInitializer(fileName));
	}

	public static void initTest() {

		instance = new BodiesManager(new TestBodyInitializer());
	}

	public static BodiesManager getInstance() {
		return instance;

	}

	public List<Body> getBodiesList() {
		return bodies;

	}

	/**
	 * Fuses the bodies. It removes from the private list the bodies contained
	 * in the list specified as argument and creates a new CompoundBody that
	 * contains all the elements removed.
	 * 
	 * @param list
	 * @throws UncompatibleDimensionsException
	 */
	public void fuseBodies(List<Body> list)
			throws UncompatibleDimensionsException {
		for (Body body : list) {
			this.bodies.remove(body);
		}
		this.bodies.add(new CompoundBody(list));
	}

	public int getBodiesCount() {
		return bodies.size();
	}
}
